//
//  FantasiaStorage.m
//  Fantasia
//
//  Created by kongkong on 16/9/17.
//  Copyright © 2016年 Kongkong. All rights reserved.
//

#import "FantasiaStorage.h"

@interface FantasiaStorage ()
@property(nonatomic, strong) NSMutableDictionary *memoryStorage;
@end

@implementation FantasiaStorage
+ (FantasiaStorage *)instance {
    static FantasiaStorage *_instance = nil;

    @synchronized (self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }

    return _instance;
}

- (instancetype)init {
    self = [super init];

    if (self) {
        @synchronized (self) {
            _memoryStorage = [[NSMutableDictionary alloc] init];
        }
    }
    return self;
}

- (void)setMemoryValue:(id)value forKey:(NSString *)key {
    NSParameterAssert(value);
    NSParameterAssert(key);
    @synchronized (self) {
        self.memoryStorage[key] = value;
    }
}

- (void)removeMemoryForKey:(NSString *)key {
    NSParameterAssert(key);
    @synchronized (self) {
        [self.memoryStorage removeObjectForKey:key];
    }
}

- (id)memoryValueForKey:(NSString *)key {
    id value = nil;
    @synchronized (self) {
        value = self.memoryStorage[key];
    }

    return value;
}

- (void)setLocalValue:(id)value forKey:(NSString *)key{
    [[NSUserDefaults standardUserDefaults]setObject:value forKey:key];
}

- (void)removeLocalForKey:(NSString *)key{
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:key];
}

- (id)localValueForKey:(NSString *)key{
    return [[NSUserDefaults standardUserDefaults]valueForKey:key];
}
@end
